package com.fourGirlsAndADude.mobileDRT;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import com.att.intern.webservice.Incident;
import com.att.intern.webservice.Webservice;
import com.att.intern.webservice.Webservice.GeolocInvalidException;
import com.att.intern.webservice.Webservice.TokenInvalidException;

import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.text.format.Time;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class AddAssesmentActivity extends Activity {
	
	//Create a new Incident class object for the new damage assessment screen
	static Incident aIncident = new Incident();
	static String picture = new String("");
	
	//5 prepopulated events from GeolocSearch, Not editable
	TextView glc;
	TextView state, pM, buildingName, address;
	//Buttons at the top of the screen and bottom of the screen
	Button addPictureTop, saveTop, cancelTop;
	Button addPictureBottom, saveBottom, cancelBottom;
	Button submitIncidentBottom;
	//Group 1: 1st group of editable fields 4 EditText fields and 3 Drop Boxes 
	EditText requesterIDedit, contactNum, initialRptDate, workReqNum;
	Spinner eventYear, eventName, propType;
	// Group 2: NON-CO Facility, group of editable fields 2 Drop Boxes 
	Spinner commercialPower, onGenerator;
	// Group 3: Any Facility, group of editable fields 5 drop boxes    
	Spinner damage, mobCo, unoccupiable, buildingStatus, creLead;
	//Group 4: Estimated Costs group of editable fields 2 EditText fields 
	EditText capital, expense;
	//Group 5: categories group of editable fields drop boxes    	 
	Spinner elecIssue, elecClosed, envirIssue, envirClosed, 
		fgIssue, fgClosed, genIssue, genClosed, waterIssue, waterClosed, groundsIssue,
		groundsClosed, mechIssue, mechClosed, otherIssue, otherClosed, plumbIssue, plumClosed,
		roofIssue, roofClosed, safeIssue, safeClosed, structIssue, structClosed;
	//Group 6: last fields group of editable fields 4 EditTexts, 1 DropBox 
	EditText assessment, status, compDate, notes;
	Spinner rep_OC;
	
	// Adapters for spinners
	private ArrayAdapter<String> yearAdapter;
	private ArrayAdapter<String> nameAdapter;

	// ArrayList for spinners
	private ArrayList<String> yearArrayList = new ArrayList<String>();
	private ArrayList<String> nameArrayList = new ArrayList<String>();

	private int yearSelected;
	private String nameSelected;
	private String reportStatusSelected;
	private String propertyTypeSelected;
	private String commercialPowerSelected;
	private String onGeneratorSelected;
	private String damageSelected, mobCoSelected, unoccupiableSelected, buildingStatusSelected, creLeadSelected;
	private String elecIssueSelected, elecClosedSelected, envirIssueSelected, envirClosedSelected, 
	fgIssueSelected, fgClosedSelected, genIssueSelected, genClosedSelected, waterIssueSelected, waterClosedSelected, groundsIssueSelected,
	groundsClosedSelected, mechIssueSelected, mechClosedSelected, otherIssueSelected, otherClosedSelected, plumbIssueSelected, plumbClosedSelected,
	roofIssueSelected, roofClosedSelected, safetyIssueSelected, safetyClosedSelected, structIssueSelected, structClosedSelected;

	ImageView add;
	
	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_assesment);
		//5 prepopulated fields
	    final int geolocint = this.getIntent().getIntExtra("GLC", 0);
	    final String sessionTokenId = this.getIntent().getStringExtra("SESSION");
	    final String userNameStr = this.getIntent().getStringExtra("USER");
		final String authorizationType = this.getIntent().getStringExtra("AUTHTYPE");

	    String statestr = this.getIntent().getStringExtra("STATE");
	    String pmStr = this.getIntent().getStringExtra("PM");
	    String buildingStr = this.getIntent().getStringExtra("BUILDINGNAME");
	    String addressStr = this.getIntent().getStringExtra("ADDRESS");
		//Toast Messages
		    	
		    	
	    Context context2 = getApplicationContext();
    	final Webservice test = new Webservice(context2);
    	final Toast success = Toast.makeText(context2, "Saved!", Toast.LENGTH_SHORT);
    	final Toast fail1 = Toast.makeText(context2, "Fail!", Toast.LENGTH_SHORT);
    	final Toast fail2= Toast.makeText(context2, "No!", Toast.LENGTH_SHORT);
	    
    	glc = (TextView)findViewById(R.id.GLC);
	    glc.setText("GLC: "+Integer.toString(geolocint));
	    state = (TextView)findViewById(R.id.State);
	    state.setText(statestr);
	    pM = (TextView)findViewById(R.id.PM);
	    pM.setText(pmStr);
	    buildingName=(TextView)findViewById(R.id.BuildingName);
	    buildingName.setText("Building Name: " +buildingStr);
	    address = (TextView)findViewById(R.id.Address);
	    address.setText(addressStr);
	    
	    //Add, save, cancel on top
	    addPictureTop=(Button)findViewById(R.id.AddPictureButtonTop);
	    addPictureTop.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
	    	
	    });
	    saveTop =(Button)findViewById(R.id.SaveButtonTop);
	    saveTop.setOnClickListener(new OnClickListener(){

	    	@Override
			public void onClick(View v) {
				Log.d("DEBUG", "Clicked on save");
	    		//Group 1
	    		String requesterId=requesterIDedit.getText().toString();
				Log.d("Incident Object", "requester id check 1: "+requesterId);
	    		String contactNumber=contactNum.getText().toString();
	    		Log.d("Incident Object", "contact num check 1: "+contactNumber);
	    		String initialReportDate=initialRptDate.getText().toString();
	    		Log.d("Incident Object", "initialreportdate check 1: "+initialReportDate);
	    		String workRecordNumber = workReqNum.getText().toString();
	    		//Group 2
				//Group 3
				//Group 4
				String estCapCost = capital.getText().toString();
				Log.d("Incident Object", "estCapCost check 1: "+estCapCost);
				String estExpenseCost = expense.getText().toString();
				Log.d("Incident Object", "estExpenseCost check 1: "+estExpenseCost);
				//Group 5
				//Group 6			
				String assessNotes = assessment.getText().toString();
				Log.d("Incident object", "assessnotes check 1: "+assessNotes);
				String statusNotes = status.getText().toString();
				Log.d("Incident object", "statusNotes check 1: "+statusNotes);
				String compltnDate = compDate.getText().toString();
				Log.d("Incident object", "completiondate check 1: "+compltnDate);
				String incidentNotes = notes.getText().toString();
				Log.d("Incident object", "incidentNotes check 1: "+incidentNotes);

				//Save all the fields to the incident object
				//Prepopulated fields
				aIncident.setGeoLoc(geolocint);
				aIncident.setState(state.getText().toString());
				aIncident.setPMAttuid(pM.getText().toString());
				aIncident.setBuildingName(buildingName.getText().toString());
				aIncident.setBuildingAddress(address.getText().toString());
				//Group 1
				try{
					aIncident.setIncidentYear(yearSelected);
					Log.d("Incident Object", "yearSelected: "+yearSelected);
				}
				catch(NumberFormatException e){
					Log.d("Catch", "Number format exception");
				}
				aIncident.setReqATTUID(requesterId);
				Log.d("Incident Object", "requester id check 2: "+requesterId);
				aIncident.setContactPhone(contactNumber);
	    		Log.d("Incident Object", "contact num check 2: "+contactNumber);
				aIncident.setInitialRptDate(initialReportDate);
	    		Log.d("Incident Object", "initialreportdate check 2: "+initialReportDate);
				aIncident.setEventName(nameSelected);
				aIncident.setBuildingType(propertyTypeSelected);
				Log.d("Incident Object", "property type check 2: "+propertyTypeSelected);
				aIncident.setWorkReqNumber(workRecordNumber);
				Log.d("Incident Object", "workRecordNum: "+workRecordNumber);
				//Group 2
				aIncident.setComPowerIndicator(commercialPowerSelected);
				Log.d("Incident Object", "commercial power check: "+commercialPowerSelected);
				aIncident.setOnGeneratorIndicator(onGeneratorSelected);
				Log.d("Incident Object", "ongenerator: "+onGeneratorSelected);
				//Group 3
				aIncident.setCreLead(creLeadSelected);
				Log.d("Incident Object", "cre lead: "+creLeadSelected);
				aIncident.setBuildingStatus(buildingStatusSelected);
				Log.d("Incident Object", "buildStat: "+buildingStatusSelected);
				aIncident.setUnOccupiableIndicator(unoccupiableSelected);
				Log.d("Incident Object", "unoccupiable: "+unoccupiableSelected);
				aIncident.setMobCOIndicator(mobCoSelected);
				Log.d("Incident Object", "mobCo: "+mobCoSelected);
				aIncident.setDamageIndicator(damageSelected);
				Log.d("Incident Object", "damage: "+damageSelected);
				//Group 4
				aIncident.setEstCapCost(Integer.parseInt(estCapCost));
				Log.d("Incident Object", "estCapCost check 2: "+estCapCost);
				aIncident.setEstExpenseCost(Integer.parseInt(estExpenseCost));
				Log.d("Incident Object", "estExpenseCost check 2: "+estExpenseCost);
				//Group 5
				aIncident.setElecIssueIndicator(elecIssueSelected);
				Log.d("Incident Object", "elecIssueSelected: "+elecIssueSelected);
				aIncident.setElecIssueClsdIndicator(elecClosedSelected);
				Log.d("Incident Object", "elecIssueSelected: "+elecClosedSelected);
				aIncident.setEnvIssueIndicator(envirIssueSelected);
				Log.d("Incident Object", "envirIssueSelected: "+envirIssueSelected);
				aIncident.setEnvIssueClsdIndicator(envirClosedSelected);
				Log.d("Incident Object", "envirClosedSelected: "+envirClosedSelected);
				aIncident.setFenceGateIssueIndicator(fgIssueSelected);
				Log.d("Incident Object", "envirClosedSelected: "+fgIssueSelected);
				aIncident.setFenceGateIssueClsdIndicator(fgClosedSelected);
				Log.d("Incident Object", "fgIssueClosed: "+fgClosedSelected);
				aIncident.setGenIssueIndicator(genIssueSelected);
				Log.d("Incident Object", "genIssueSelected: "+genIssueSelected);
				aIncident.setGenIssueClsdIndicator(genClosedSelected);
				Log.d("Incident Object", "genClosedSelected: "+genClosedSelected);

				aIncident.setWaterIssueIndicator(waterIssueSelected);
				Log.d("Incident Object", "waterIssueSelected: "+waterIssueSelected);
				aIncident.setWaterIssueClsdIndicator(waterClosedSelected);
				Log.d("Incident Object", "waterClosedSelected: "+waterClosedSelected);
				aIncident.setGroundsIssueIndicator(groundsIssueSelected);
				Log.d("Incident Object", "groundsIssueSelected: "+groundsIssueSelected);
				aIncident.setGroundsIssueClsdIndicator(groundsClosedSelected);
				Log.d("Incident Object", "groundsClosedSelected: "+groundsClosedSelected);
				aIncident.setMechIssueIndicator(mechIssueSelected);
				Log.d("Incident Object", "mechIssueSelected: "+mechIssueSelected);
				aIncident.setMechIssueClsdIndicator(mechClosedSelected);
				Log.d("Incident Object", "mechClosedSelected: "+mechClosedSelected);
				aIncident.setOtherIssueIndicator(otherIssueSelected);
				Log.d("Incident Object", "otherIssueSelected: "+otherIssueSelected);
				aIncident.setOtherIssueClsdIndicator(otherClosedSelected);
				Log.d("Incident Object", "otherClosedSelected: "+otherClosedSelected);
				aIncident.setPlumbIssueIndicator(plumbIssueSelected);
				Log.d("Incident Object", "plumbIssueSelected: "+plumbIssueSelected);
				aIncident.setPlumbIssueClsdIndicator(plumbClosedSelected);
				Log.d("Incident Object", "plumbClosedSelected: "+plumbClosedSelected);
				aIncident.setRoofsIssueIndicator(roofIssueSelected);
				Log.d("Incident Object", "roofIssueSelected: "+roofIssueSelected);
				aIncident.setRoofsIssueClsdIndicator(roofClosedSelected);
				Log.d("Incident Object", "roofClosedSelected: "+roofClosedSelected);
				aIncident.setSafetyIssueIndicator(safetyIssueSelected);
				Log.d("Incident Object", "safetyIssueSelected: "+safetyIssueSelected);
				aIncident.setSafetyIssueClsdIndicator(safetyClosedSelected);
				Log.d("Incident Object", "safetyClosedSelected: "+safetyClosedSelected);
				aIncident.setStructIssueIndicator(structIssueSelected);
				Log.d("Incident Object", "structIssueSelected: "+structIssueSelected);
				aIncident.setStructIssueClsdIndicator(structClosedSelected);
				Log.d("Incident Object", "structClosedSelected: "+structClosedSelected);
				//Group 6
				aIncident.setAssessNotes(assessNotes);
				Log.d("Incident Object", "assessNotes check 2: "+assessNotes);
				aIncident.setStatusNotes(statusNotes);
				Log.d("Incident Object", "statusNotes check 2: "+statusNotes);
				aIncident.setCompltnDate(compltnDate);
				Log.d("Incident Object", "compltnDate check 2: "+compltnDate);
				aIncident.setIncidentNotes(incidentNotes);
				Log.d("Incident Object", "incidentNotes check 2: "+incidentNotes);
				aIncident.setIncidentStatus(reportStatusSelected);
				Log.d("Incident Object", "report status check 2: "+reportStatusSelected);
				
				//Try to add the incident
				try {
					Log.d("DEBUG", "Entered try");
					Log.d("Print session", "SessionToken: "+sessionTokenId);

					test.addIncident(sessionTokenId, aIncident);
					success.show();
					Intent i = new Intent(getBaseContext(),
							GeolocSearchActivity.class);
					i.putExtra("AUTHORIZATION_TYPE",authorizationType);
					i.putExtra("TOKEN_ID", sessionTokenId);
					i.putExtra("USER_NAME", userNameStr);
		    		startActivity(i);
				} 
				catch(NullPointerException e){
					e.printStackTrace();
				}
				catch (TokenInvalidException e) {
					Log.d("DEBUG", "Entered catch TokenInvalidException");
					fail1.show();
					e.printStackTrace();
				} catch (GeolocInvalidException e) {
					Log.d("DEBUG", "Entered catch GeolocInvalidException");
					fail2.show();
					e.printStackTrace();
				}
			}//end onclick
	    	
	    });
	    cancelTop=(Button)findViewById(R.id.CancelButtonTop);
		cancelTop.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
    		Log.d("AddAssessmentActivity", "Pressed cancel");
    		
    		fail2.show();
			Intent i = new Intent();
			i.setClassName("com.fourGirlsAndADude.mobileDRT", "com.fourGirlsAndADude.mobileDRT.LoginActivity");
			startActivity(i);
			finish();
			}
			
		});
	    addPictureBottom=(Button)findViewById(R.id.AddPictureButtonBottom);
	    addPictureBottom.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				picture();
				
			}
	    	
	    });
	    saveBottom =(Button)findViewById(R.id.SaveButtonBottom);
	    saveBottom.setOnClickListener(new OnClickListener(){

	    	@Override
			public void onClick(View v) {
				Log.d("DEBUG", "Clicked on save");
	    		//Group 1
	    		String requesterId=requesterIDedit.getText().toString();
				Log.d("Incident Object", "requester id check 1: "+requesterId);
	    		String contactNumber=contactNum.getText().toString();
	    		Log.d("Incident Object", "contact num check 1: "+contactNumber);
	    		String initialReportDate=initialRptDate.getText().toString();
	    		Log.d("Incident Object", "initialreportdate check 1: "+initialReportDate);
	    		String workRecordNumber = workReqNum.getText().toString();
	    		//Group 2
				//Group 3
				//Group 4
				String estCapCost = capital.getText().toString();
				Log.d("Incident Object", "estCapCost check 1: "+estCapCost);
				String estExpenseCost = expense.getText().toString();
				Log.d("Incident Object", "estExpenseCost check 1: "+estExpenseCost);
				//Group 5
				//Group 6			
				String assessNotes = assessment.getText().toString();
				Log.d("Incident object", "assessnotes check 1: "+assessNotes);
				String statusNotes = status.getText().toString();
				Log.d("Incident object", "statusNotes check 1: "+statusNotes);
				String compltnDate = compDate.getText().toString();
				Log.d("Incident object", "completiondate check 1: "+compltnDate);
				String incidentNotes = notes.getText().toString();
				Log.d("Incident object", "incidentNotes check 1: "+incidentNotes);

				//Save all the fields to the incident object
				//Prepopulated fields
				aIncident.setGeoLoc(geolocint);
				aIncident.setState(state.getText().toString());
				aIncident.setPMAttuid(pM.getText().toString());
				aIncident.setBuildingName(buildingName.getText().toString());
				aIncident.setBuildingAddress(address.getText().toString());
				//Group 1
				try{
					aIncident.setIncidentYear(yearSelected);
					Log.d("Incident Object", "yearSelected: "+yearSelected);
				}
				catch(NumberFormatException e){
					Log.d("Catch", "Number format exception");
				}
				aIncident.setReqATTUID(requesterId);
				Log.d("Incident Object", "requester id check 2: "+requesterId);
				aIncident.setContactPhone(contactNumber);
	    		Log.d("Incident Object", "contact num check 2: "+contactNumber);
				aIncident.setInitialRptDate(initialReportDate);
	    		Log.d("Incident Object", "initialreportdate check 2: "+initialReportDate);
				aIncident.setEventName(nameSelected);
				aIncident.setBuildingType(propertyTypeSelected);
				Log.d("Incident Object", "property type check 2: "+propertyTypeSelected);
				aIncident.setWorkReqNumber(workRecordNumber);
				Log.d("Incident Object", "workRecordNum: "+workRecordNumber);
				//Group 2
				aIncident.setComPowerIndicator(commercialPowerSelected);
				Log.d("Incident Object", "commercial power check: "+commercialPowerSelected);
				aIncident.setOnGeneratorIndicator(onGeneratorSelected);
				Log.d("Incident Object", "ongenerator: "+onGeneratorSelected);
				//Group 3
				aIncident.setCreLead(creLeadSelected);
				Log.d("Incident Object", "cre lead: "+creLeadSelected);
				aIncident.setBuildingStatus(buildingStatusSelected);
				Log.d("Incident Object", "buildStat: "+buildingStatusSelected);
				aIncident.setUnOccupiableIndicator(unoccupiableSelected);
				Log.d("Incident Object", "unoccupiable: "+unoccupiableSelected);
				aIncident.setMobCOIndicator(mobCoSelected);
				Log.d("Incident Object", "mobCo: "+mobCoSelected);
				aIncident.setDamageIndicator(damageSelected);
				Log.d("Incident Object", "damage: "+damageSelected);
				//Group 4
				aIncident.setEstCapCost(Integer.parseInt(estCapCost));
				Log.d("Incident Object", "estCapCost check 2: "+estCapCost);
				aIncident.setEstExpenseCost(Integer.parseInt(estExpenseCost));
				Log.d("Incident Object", "estExpenseCost check 2: "+estExpenseCost);
				//Group 5
				aIncident.setElecIssueIndicator(elecIssueSelected);
				Log.d("Incident Object", "elecIssueSelected: "+elecIssueSelected);
				aIncident.setElecIssueClsdIndicator(elecClosedSelected);
				Log.d("Incident Object", "elecIssueSelected: "+elecClosedSelected);
				aIncident.setEnvIssueIndicator(envirIssueSelected);
				Log.d("Incident Object", "envirIssueSelected: "+envirIssueSelected);
				aIncident.setEnvIssueClsdIndicator(envirClosedSelected);
				Log.d("Incident Object", "envirClosedSelected: "+envirClosedSelected);
				aIncident.setFenceGateIssueIndicator(fgIssueSelected);
				Log.d("Incident Object", "envirClosedSelected: "+fgIssueSelected);
				aIncident.setFenceGateIssueClsdIndicator(fgClosedSelected);
				Log.d("Incident Object", "fgIssueClosed: "+fgClosedSelected);
				aIncident.setGenIssueIndicator(genIssueSelected);
				Log.d("Incident Object", "genIssueSelected: "+genIssueSelected);
				aIncident.setGenIssueClsdIndicator(genClosedSelected);
				Log.d("Incident Object", "genClosedSelected: "+genClosedSelected);

				aIncident.setWaterIssueIndicator(waterIssueSelected);
				Log.d("Incident Object", "waterIssueSelected: "+waterIssueSelected);
				aIncident.setWaterIssueClsdIndicator(waterClosedSelected);
				Log.d("Incident Object", "waterClosedSelected: "+waterClosedSelected);
				aIncident.setGroundsIssueIndicator(groundsIssueSelected);
				Log.d("Incident Object", "groundsIssueSelected: "+groundsIssueSelected);
				aIncident.setGroundsIssueClsdIndicator(groundsClosedSelected);
				Log.d("Incident Object", "groundsClosedSelected: "+groundsClosedSelected);
				aIncident.setMechIssueIndicator(mechIssueSelected);
				Log.d("Incident Object", "mechIssueSelected: "+mechIssueSelected);
				aIncident.setMechIssueClsdIndicator(mechClosedSelected);
				Log.d("Incident Object", "mechClosedSelected: "+mechClosedSelected);
				aIncident.setOtherIssueIndicator(otherIssueSelected);
				Log.d("Incident Object", "otherIssueSelected: "+otherIssueSelected);
				aIncident.setOtherIssueClsdIndicator(otherClosedSelected);
				Log.d("Incident Object", "otherClosedSelected: "+otherClosedSelected);
				aIncident.setPlumbIssueIndicator(plumbIssueSelected);
				Log.d("Incident Object", "plumbIssueSelected: "+plumbIssueSelected);
				aIncident.setPlumbIssueClsdIndicator(plumbClosedSelected);
				Log.d("Incident Object", "plumbClosedSelected: "+plumbClosedSelected);
				aIncident.setRoofsIssueIndicator(roofIssueSelected);
				Log.d("Incident Object", "roofIssueSelected: "+roofIssueSelected);
				aIncident.setRoofsIssueClsdIndicator(roofClosedSelected);
				Log.d("Incident Object", "roofClosedSelected: "+roofClosedSelected);
				aIncident.setSafetyIssueIndicator(safetyIssueSelected);
				Log.d("Incident Object", "safetyIssueSelected: "+safetyIssueSelected);
				aIncident.setSafetyIssueClsdIndicator(safetyClosedSelected);
				Log.d("Incident Object", "safetyClosedSelected: "+safetyClosedSelected);
				aIncident.setStructIssueIndicator(structIssueSelected);
				Log.d("Incident Object", "structIssueSelected: "+structIssueSelected);
				aIncident.setStructIssueClsdIndicator(structClosedSelected);
				Log.d("Incident Object", "structClosedSelected: "+structClosedSelected);
				//Group 6
				aIncident.setAssessNotes(assessNotes);
				Log.d("Incident Object", "assessNotes check 2: "+assessNotes);
				aIncident.setStatusNotes(statusNotes);
				Log.d("Incident Object", "statusNotes check 2: "+statusNotes);
				aIncident.setCompltnDate(compltnDate);
				Log.d("Incident Object", "compltnDate check 2: "+compltnDate);
				aIncident.setIncidentNotes(incidentNotes);
				Log.d("Incident Object", "incidentNotes check 2: "+incidentNotes);
				aIncident.setIncidentStatus(reportStatusSelected);
				Log.d("Incident Object", "report status check 2: "+reportStatusSelected);
				
				//Try to add the incident
				try {
					Log.d("DEBUG", "Entered try");
					Log.d("Print session", "SessionToken: "+sessionTokenId);

					test.addIncident(sessionTokenId, aIncident);
					success.show();
//					Intent i = new Intent(getBaseContext(),
//							GeolocSearchActivity.class);
//					i.putExtra("AUTHORIZATION_TYPE",authorizationType);
//					i.putExtra("TOKEN_ID", sessionTokenId);
//					i.putExtra("USER_NAME", userNameStr);
//		    		startActivity(i);
				} 
				catch(NullPointerException e){
					e.printStackTrace();
				}
				catch (TokenInvalidException e) {
					Log.d("DEBUG", "Entered catch TokenInvalidException");
					fail1.show();
					e.printStackTrace();
				} catch (GeolocInvalidException e) {
					Log.d("DEBUG", "Entered catch GeolocInvalidException");
					fail2.show();
					e.printStackTrace();
				}
			}//end onclick
	    	
	    });
	    cancelBottom=(Button)findViewById(R.id.CancelButtonBottom);
		cancelBottom.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
			Log.d("AddAssessmentActivity", "Pressed cancel");
    		
    		fail2.show();
			Intent i = new Intent();
			i.setClassName("com.fourGirlsAndADude.mobileDRT", "com.fourGirlsAndADude.mobileDRT.LoginActivity");
			startActivity(i);
			finish();
			}
			
		});
		submitIncidentBottom=(Button)findViewById(R.id.SubmitIncidentButtonBottom);
		submitIncidentBottom.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(getBaseContext(),
				GeolocSearchActivity.class);
				i.putExtra("PictureFile", picture);
				i.putExtra("AUTHORIZATION_TYPE",authorizationType);
				i.putExtra("TOKEN_ID", sessionTokenId);
				i.putExtra("USER_NAME", userNameStr);
				startActivity(i);
			}
			
		});
		
		//Group 1
		eventYear=(Spinner)findViewById(R.id.eventYear_dropdown);
		Log.v("RS", "1");
		intializeLists();
		Log.v("RS", "6");
		yearAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_dropdown_item, yearArrayList);
		Log.v("RS", "7");
		eventYear.setAdapter(yearAdapter);
		Log.v("RS", "8");
		eventYear.setOnItemSelectedListener(new EventYearOnSelectedItemClickListner());
		
		
//		eventYear.setSelection(0);
		requesterIDedit =(EditText)findViewById(R.id.requesterIDinput);
		requesterIDedit.setText(userNameStr, TextView.BufferType.EDITABLE);
		contactNum = (EditText)findViewById(R.id.contactNumber);
		initialRptDate=(EditText)findViewById(R.id.CurrentDate);
		//Resource: http://stackoverflow.com/questions/8654990/how-can-i-get-current-date-in-android
		Calendar c = Calendar.getInstance();
		System.out.println("Current time => " + c.getTime());
		SimpleDateFormat df = new SimpleDateFormat("MMM-dd-yyyy");
		String formattedDate = df.format(c.getTime());
		initialRptDate.setText(formattedDate, TextView.BufferType.EDITABLE);
		eventName=(Spinner)findViewById(R.id.eventType_dropdown);
		//Dynamically handle the eventName spinner depending on what year entered
		nameAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_dropdown_item, nameArrayList);
		eventName.setAdapter(nameAdapter);
		eventName.setOnItemSelectedListener(new EventNameOnSelectedItemClickListner());
		
		propType=(Spinner)findViewById(R.id.PropertyType_dropdown);
		propType.setOnItemSelectedListener(new PropertyTypeOnSelectedItemClickListener());
		workReqNum = (EditText)findViewById(R.id.WorkRequestNumber);
		
		// Group 2 
		commercialPower=(Spinner)findViewById(R.id.commercialPower);
		commercialPower.setOnItemSelectedListener(new CommercialPowerOnSelectedItemClickListener());
		onGenerator = (Spinner)findViewById(R.id.onGenerator);
		onGenerator.setOnItemSelectedListener(new onGeneratorOnItemSelectedListener());

		
		//Group 3
		damage= (Spinner)findViewById(R.id.damaged);
		damage.setOnItemSelectedListener(new DamagedOnItemSelectedListener());
		mobCo = (Spinner)findViewById(R.id.mobilityCO);
		mobCo.setOnItemSelectedListener(new mobCoOnItemSelectedListener());
		unoccupiable =(Spinner)findViewById(R.id.unoccupiable);
		unoccupiable.setOnItemSelectedListener(new unoccupiableOnItemSelectedListener());
		buildingStatus=(Spinner)findViewById(R.id.buildingStatus);
		buildingStatus.setOnItemSelectedListener(new buildingStatusOnItemSelectedListener());
		creLead =(Spinner)findViewById(R.id.crelead);
		creLead.setOnItemSelectedListener(new creLeadOnItemSelectedListener());
		
		//Group 4  
		capital=(EditText)findViewById(R.id.capital);
		expense=(EditText)findViewById(R.id.expense);
		
		//Group 5 
		elecIssue=(Spinner)findViewById(R.id.issueElectric);
		elecIssue.setOnItemSelectedListener(new elecIssueOnItemSelectedListener());
    	elecClosed =(Spinner)findViewById(R.id.closedElectric);
		elecClosed.setOnItemSelectedListener(new elecClosedOnItemSelectedListener());
    	envirIssue =(Spinner)findViewById(R.id.issueEnviron);
		envirIssue.setOnItemSelectedListener(new envirIssueOnItemSelectedListener());
    	envirClosed=(Spinner)findViewById(R.id.closedEnviron);
		envirClosed.setOnItemSelectedListener(new envirClosedOnItemSelectedListener());
    	fgIssue=(Spinner)findViewById(R.id.issueFence);
    	fgIssue.setOnItemSelectedListener(new fgIssueOnItemSelectedListener());
    	fgClosed=(Spinner)findViewById(R.id.closedFence);
    	fgClosed.setOnItemSelectedListener(new fgClosedOnItemSelectedListener());
    	genIssue=(Spinner)findViewById(R.id.issueGen);
    	genIssue.setOnItemSelectedListener(new genIssueOnItemSelectedListener());
    	genClosed=(Spinner)findViewById(R.id.closedGen);
    	genClosed.setOnItemSelectedListener(new genClosedOnItemSelectedListener());

    	waterIssue=(Spinner)findViewById(R.id.issueWater);
    	waterIssue.setOnItemSelectedListener(new waterIssueOnItemSelectedListener());
    	waterClosed=(Spinner)findViewById(R.id.closedWater);
    	waterClosed.setOnItemSelectedListener(new waterClosedOnItemSelectedListener());
    	groundsIssue=(Spinner)findViewById(R.id.issueGround);
    	groundsIssue.setOnItemSelectedListener(new groundsIssueOnItemSelectedListener());
    	groundsClosed=(Spinner)findViewById(R.id.closedGround);
    	groundsClosed.setOnItemSelectedListener(new groundsClosedOnItemSelectedListener());
    	mechIssue =(Spinner)findViewById(R.id.issueMech);
    	mechIssue.setOnItemSelectedListener(new mechIssueOnItemSelectedListener());
    	mechClosed=(Spinner)findViewById(R.id.closedMech);
    	mechClosed.setOnItemSelectedListener(new mechClosedOnItemSelectedListener());
    	otherIssue=(Spinner)findViewById(R.id.issueOther);
    	otherIssue.setOnItemSelectedListener(new otherIssueOnItemSelectedListener());
    	otherClosed=(Spinner)findViewById(R.id.closedOther);
    	otherClosed.setOnItemSelectedListener(new otherClosedOnItemSelectedListener());
    	plumbIssue=(Spinner)findViewById(R.id.issuePlumb);
    	plumbIssue.setOnItemSelectedListener(new plumbIssueOnItemSelectedListener());
    	plumClosed=(Spinner)findViewById(R.id.closedPlumb);
    	plumClosed.setOnItemSelectedListener(new plumbClosedOnItemSelectedListener());
    	roofIssue=(Spinner)findViewById(R.id.issueRoof);
    	roofIssue.setOnItemSelectedListener(new roofIssueOnItemSelectedListener());
    	roofClosed=(Spinner)findViewById(R.id.closedRoof);
    	roofClosed.setOnItemSelectedListener(new roofClosedOnItemSelectedListener());
    	safeIssue=(Spinner)findViewById(R.id.issueSafe);
    	safeIssue.setOnItemSelectedListener(new safeIssueOnItemSelectedListener());
    	safeClosed=(Spinner)findViewById(R.id.closedSafe);
    	safeClosed.setOnItemSelectedListener(new safeClosedOnItemSelectedListener());
    	structIssue=(Spinner)findViewById(R.id.issueStruc);
    	structIssue.setOnItemSelectedListener(new structIssueOnItemSelectedListener());
    	structClosed=(Spinner)findViewById(R.id.closedStruc);
    	structClosed.setOnItemSelectedListener(new structClosedOnItemSelectedListener());
    	
    	//Group 6: last fields group of editable fields 4 EditTexts, 1 DropBox 
    	assessment=(EditText)findViewById(R.id.assessment);
    	status=(EditText)findViewById(R.id.status);
    	rep_OC=(Spinner)findViewById(R.id.reportO_C);
    	rep_OC.setOnItemSelectedListener(new ReportOpenOnItemSelectedListener());

    	compDate=(EditText)findViewById(R.id.completionDate); 
    	compDate.setText(formattedDate, TextView.BufferType.EDITABLE);
    	notes=(EditText)findViewById(R.id.notes);
    	
    	//Image
    	add=(ImageView)findViewById(R.id.imageView1);
	}//end of oncreate
	
	private class EventYearOnSelectedItemClickListner implements
			OnItemSelectedListener {

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// Get the year selected from the spinner
			Log.v("RS", "8A");
			int data;
			try {
				Log.v("RS", "8B");
				data = Integer.valueOf(eventYear.getItemAtPosition(arg2)
						.toString());
			} catch (NumberFormatException e) {
				data = 0;
			}
			Log.v("RS", "Data: " + data);
			// Depending on which year has been selected intialize the
			// new adapter and set it for the event name spinner
			switch (data) {
			case 2010:
				Log.v("RS", "Case 2010");
				intializeList(getResources().getStringArray(
						R.array.eventName2010));
				break;
			case 2011:
				Log.v("RS", "Case 2011");
				intializeList(getResources().getStringArray(
						R.array.eventName2011));
				break;
			case 2012:
				Log.v("RS", "Case 2012");
				intializeList(getResources().getStringArray(
						R.array.eventName2012));

				break;
			case 2013:
				Log.v("RS", "Case 2013");
				intializeList(getResources().getStringArray(
						R.array.eventName2013));
				break;
			default:
				Log.v("RS", "Case default");
				intializeList(getResources().getStringArray(R.array.plzSelect));
				break;
			}

			yearSelected = data;
			Log.v("RS", "recreating adapter");

			nameAdapter = new ArrayAdapter<String>(AddAssesmentActivity.this,
					android.R.layout.simple_list_item_1, nameArrayList);

			Log.v("RS", "resetting adapter");
			eventName.setAdapter(nameAdapter);

			Log.v("RS", "8C");
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
		}
	}

	private class EventNameOnSelectedItemClickListner implements
			OnItemSelectedListener {
		String[] categories = { "HURRNAME", "EARTHQUAKE", "FLOOD", "ICE STORM",
				"SNOW STORM", "TORNADO", "WILDFIRE" };
		Boolean isACategory = false;

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			String selectedItem = (String) arg0.getItemAtPosition(arg2);
			for (String s : categories) {
				if (s.equals(selectedItem)) {
					isACategory = true;
					break;
				}
			}

			if (!isACategory) {
				nameSelected = (String) arg0.getItemAtPosition(arg2);
			} else {
				nameSelected = null;
			}

		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
		}
	}

	private void intializeLists() {
		Log.v("RS", "2");
		// Initialize ArrayList for event Year
		String[] yearStrings = getResources().getStringArray(R.array.eventYear);
		Log.v("RS", "3");
		for (int i = 0; i < yearStrings.length; i++) {
			yearArrayList.add(yearStrings[i]);
		}

		// Initialize ArrayList
		Log.v("RS", "4");
		String[] nameStrings = getResources().getStringArray(R.array.plzSelect);
		for (int i = 0; i < nameStrings.length; i++) {
			nameArrayList.add(nameStrings[i]);
		}

		Log.v("RS", "5");

	}

	private void intializeList(String[] stringArray) {
		nameArrayList.clear();
		for (String s : stringArray) {
			nameArrayList.add(s);
		}
	}
	
	private class ReportOpenOnItemSelectedListener implements
	OnItemSelectedListener {
		
		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			reportStatusSelected=(String)arg0.getItemAtPosition(arg2);
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
		}
	}
	
	private class PropertyTypeOnSelectedItemClickListener implements OnItemSelectedListener {

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			propertyTypeSelected=(String)arg0.getItemAtPosition(arg2);
			
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
		}
		
	}
	private class CommercialPowerOnSelectedItemClickListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			commercialPowerSelected=(String)arg0.getItemAtPosition(arg2);
			
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
		}
		
	}
	private class onGeneratorOnItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			onGeneratorSelected=(String)arg0.getItemAtPosition(arg2);
			
		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
		
	}
	private class DamagedOnItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			damageSelected=(String)arg0.getItemAtPosition(arg2);
			
		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
	}
	private class mobCoOnItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			mobCoSelected=(String)arg0.getItemAtPosition(arg2);
			
		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
		
	}
	private class unoccupiableOnItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			unoccupiableSelected=(String)arg0.getItemAtPosition(arg2);
			
		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
		
	}
	private class buildingStatusOnItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			buildingStatusSelected=(String)arg0.getItemAtPosition(arg2);
			
		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
		
	}
	private class creLeadOnItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			creLeadSelected=(String)arg0.getItemAtPosition(arg2);
			
		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
		
	}
	private class elecIssueOnItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			elecIssueSelected=(String)arg0.getItemAtPosition(arg2);
			
		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
		
	}
	private class elecClosedOnItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			elecClosedSelected=(String)arg0.getItemAtPosition(arg2);
			
		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
		
	}
	private class envirIssueOnItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			envirIssueSelected=(String)arg0.getItemAtPosition(arg2);
			
		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
		
	}
	private class envirClosedOnItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			envirClosedSelected=(String)arg0.getItemAtPosition(arg2);
			
		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
		
	}
	private class fgIssueOnItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			fgIssueSelected=(String)arg0.getItemAtPosition(arg2);
			
		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
		
	}
	private class fgClosedOnItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			fgClosedSelected=(String)arg0.getItemAtPosition(arg2);
			
		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
		
	}
	private class genIssueOnItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			genIssueSelected=(String)arg0.getItemAtPosition(arg2);
			
		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
		
	}
	private class genClosedOnItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			genClosedSelected=(String)arg0.getItemAtPosition(arg2);
			
		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
		
	}
	private class waterIssueOnItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			waterIssueSelected=(String)arg0.getItemAtPosition(arg2);
			
		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
		
	}
	private class waterClosedOnItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			waterClosedSelected=(String)arg0.getItemAtPosition(arg2);
			
		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
		
	}
	private class groundsIssueOnItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			groundsIssueSelected=(String)arg0.getItemAtPosition(arg2);
			
		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
		
	}
	private class groundsClosedOnItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			groundsClosedSelected=(String)arg0.getItemAtPosition(arg2);
			
		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
		
	}
	private class mechIssueOnItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			mechIssueSelected=(String)arg0.getItemAtPosition(arg2);
			
		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
		
	}
	private class mechClosedOnItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			mechClosedSelected=(String)arg0.getItemAtPosition(arg2);
			
		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
		
	}
	private class otherIssueOnItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			otherIssueSelected=(String)arg0.getItemAtPosition(arg2);
			
		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
		
	}
	private class otherClosedOnItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			otherClosedSelected=(String)arg0.getItemAtPosition(arg2);
			
		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
		
	}
	private class plumbIssueOnItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			plumbIssueSelected=(String)arg0.getItemAtPosition(arg2);
			
		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
		
	}
	private class plumbClosedOnItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			plumbClosedSelected=(String)arg0.getItemAtPosition(arg2);
			
		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
		
	}
	private class roofIssueOnItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			roofIssueSelected=(String)arg0.getItemAtPosition(arg2);
			
		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
		
	}
	private class roofClosedOnItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			roofClosedSelected=(String)arg0.getItemAtPosition(arg2);
			
		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
		
	}
	private class safeIssueOnItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			safetyIssueSelected=(String)arg0.getItemAtPosition(arg2);
			
		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
		
	}
	private class safeClosedOnItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			safetyClosedSelected=(String)arg0.getItemAtPosition(arg2);
			
		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
		
	}
	private class structIssueOnItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			structIssueSelected=(String)arg0.getItemAtPosition(arg2);
			
		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
		
	}
	private class structClosedOnItemSelectedListener implements OnItemSelectedListener{

		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			structClosedSelected=(String)arg0.getItemAtPosition(arg2);
			
		}
		@Override
		public void onNothingSelected(AdapterView<?> arg0) {

		}
		
	}
	
	/**
	 * Takes a picture
	 */
	public void picture(){
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        
        startActivityForResult(cameraIntent, 0); 
        Log.d("AddDamage", "Camera End");
    }

	 /**
	 * Saves the picture after a picture is taken
	 * @param requestCode the request code of the Activity
	 * @param resultCode the result code of the Activity
	 * @param data the data returned by the Activity
	 */
	 protected void onActivityResult(int requestCode, int resultCode, Intent data) {   
	        if (requestCode == 0) {   
	               Log.d("UpdateDamage", "Camera Data Recieved");
	               if(data!=null && data.getExtras()!=null){
	                     Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
	                     //InputStream stream = getContentResolver().openInputStream(data.getData());
	                     MediaStore.Images.Media.insertImage(getContentResolver(), thumbnail, "Picture: "+aIncident.getRecNumber(), "Incident Picture"); 
	                     add.setImageBitmap(thumbnail);
	                     
	                     //save picture
	                     ByteArrayOutputStream stream = new ByteArrayOutputStream();
	                     thumbnail.compress(Bitmap.CompressFormat.PNG, 100, stream);
	                     byte[] byteArray = stream.toByteArray();
	                     
	                     
	                     
	                     File pictureFileDir = getDir();

	                     if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {

	                       Log.d("DEBUG_TAG", "Can't create directory to save image.");
	                       Toast.makeText(getApplicationContext(), "Can't create directory to save image.",
	                           Toast.LENGTH_LONG).show();
	                       return;

	                     }

	                     //SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
	                     //String date = dateFormat.format(new Date());
	                     String photoFile = "Picture_" + aIncident.getRecNumber() + ".jpg";
	                     System.out.println("Photofile: "+photoFile);

	                     String filename = pictureFileDir.getPath() + File.separator + photoFile;
	                     System.out.println("filename: "+filename);
	                     picture = filename;
	                     File pictureFile = new File(filename);
	                     

	                     try {
	                       FileOutputStream fos = new FileOutputStream(pictureFile);
	                       fos.write(byteArray);
	                       fos.close();
	                       Toast.makeText(getApplicationContext(), "New Image saved:" + photoFile,
	                           Toast.LENGTH_LONG).show();
	                     } 
	                     catch (Exception error) {
	                       Log.d("DEBUG_TAG", "File" + filename + "not saved: "
	                           + error.getMessage());
	                       Toast.makeText(getApplicationContext(), "Image could not be saved.",
	                           Toast.LENGTH_LONG).show();
	                     }
	                   
	                     
	               }

	                   
	                   
	               
	        }
	        Toast picture = Toast.makeText(getApplicationContext(), "Picture taken!", Toast.LENGTH_SHORT);
	        picture.show(); //Toast message
	        
	 }
	 private File getDir() {
         //File sdDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
         //return new File(sdDir, "AddCamera");
        File sdDir = null;
		try {
			sdDir = new File(Environment.getExternalStorageDirectory().getCanonicalPath() + File.separatorChar
			         + Settings.STORAGE_DIRECTORY);
			//sdDir = new File(Envionrment.)
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sdDir;
     }
	
	
}//end of class
